package mi.payment.transaction.revoke;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import java.util.List;

import lainey.ui.widget.TitleView;
import mi.payment.R;
import mi.payment.db.dao.PayDetail;
import mi.payment.ui.base.ParentActivity;
import sunmi.payment.L3.L3BroadcastHelper;
import sunmi.payment.L3.L3ErrorCode;

public abstract class BaseRevokeListActivity extends ParentActivity {

    public abstract List<PayDetail> getAllSourceData();

    public abstract List<PayDetail> getQRCodeSourceData();

    public abstract List<PayDetail> getSwipeCardSourceData();

    public abstract void onSearchPressed();

    public abstract boolean isPreAuthorizeCompleteRevoke();

    public void setTitle() {

    }

    // 0为全部，1为刷卡，2为二维码
    private int action;
    private final int TYPE_ALL = 0;
    private final int TYPE_CARD = 1;
    private final int TYPE_CODE = 2;

    private List<PayDetail> mAllList;
    private List<PayDetail> mSwipeList;
    private List<PayDetail> mQRCodeLists;
    private RevokeAdapter mAdapter;

    private TextView mTvAll;
    private TextView mTvQRCode;
    private TextView mTvBankCard;
    private ListView mListView;
    private LinearLayout mNoDataView;

    public TitleView mTitleView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.transaction_revoke_activity_main);
        initView();
        initData();
    }

    private void initView() {
        mTitleView = findViewById(R.id.title_view);
        mTitleView.setRightImageResource(R.drawable.ic_search_white_x48);
        mTitleView.setLeftImageOnClickListener(
                v -> onBackPressed()
        );
        mTitleView.setRightImageOnClickListener(
                v -> onSearchPressed()
        );
        mTitleView.setRightImageVisibility(View.VISIBLE);

        View tabView = findViewById(R.id.view_tab);

        mTvAll = findViewById(R.id.tv_all);
        mTvBankCard = findViewById(R.id.tv_bank);
        mTvQRCode = findViewById(R.id.tv_qr_code);
        mTvAll.requestFocus();
        mTvAll.setOnClickListener(this);
        mTvBankCard.setOnClickListener(this);
        mTvQRCode.setOnClickListener(this);

        mNoDataView = findViewById(R.id.view_no_data);
        mAdapter = new RevokeAdapter(this);
        mListView = findViewById(R.id.lv_purchase_cancellation);
        mListView.setAdapter(mAdapter);

        // 是否为预授权完成撤销
        boolean isPreAuthorizeCompleteRevoke = isPreAuthorizeCompleteRevoke();
        if (isPreAuthorizeCompleteRevoke) {
            tabView.setVisibility(View.GONE);
        }
        setTitle();
    }

    @Override
    protected void onResume() {
        super.onResume();
        initData();
    }

    @Override
    public void onClick(View v) {
        final int id = v.getId();
        switch (id) {
            case R.id.tv_qr_code:
                action = TYPE_CODE;
                updateUI();
                changeTextView(mTvQRCode, R.drawable.platform_qr_code_focus);
                changeHaveDataView(mQRCodeLists);
                break;
            case R.id.tv_bank:
                action = TYPE_CARD;
                updateUI();
                changeTextView(mTvBankCard, R.drawable.platform_bank_card_focus);
                changeHaveDataView(mSwipeList);
                break;
            case R.id.tv_all:
                action = TYPE_ALL;
                updateUI();
                changeTextView(mTvAll, 0);
                changeHaveDataView(mAllList);
                break;
        }
    }

    @Override
    public void onBackPressed() {
        L3BroadcastHelper.sendFailedBroadcast(mContext, L3ErrorCode.ERROR_TRADE_CANCEL);
        super.onBackPressed();
    }

    private void changeTextView(TextView view, int resId) {
        view.setBackgroundResource(R.drawable.tab_down);
        int color = getResources().getColor(R.color.colorPrimary);
        view.setTextColor(color);
        if (resId != 0) {
            Drawable img = getResources().getDrawable(resId);
            int minimumWidth = img.getMinimumWidth();
            int minimumHeight = img.getMinimumHeight();
            img.setBounds(0, 0, minimumWidth, minimumHeight);
            view.setCompoundDrawables(img, null, null, null);
        }
    }

    private void updateUI() {
        int color = getResources().getColor(R.color.white);
        mTvAll.setBackgroundResource(R.drawable.tab_up);
        mTvQRCode.setBackgroundResource(R.drawable.tab_up);
        mTvBankCard.setBackgroundResource(R.drawable.tab_up);
        mTvAll.setTextColor(color);
        mTvQRCode.setTextColor(color);
        mTvBankCard.setTextColor(color);

        Drawable img = getResources().getDrawable(R.drawable.platform_qr_code);
        int minimumWidth = img.getMinimumWidth();
        int minimumHeight = img.getMinimumHeight();
        img.setBounds(0, 0, minimumWidth, minimumHeight);
        mTvQRCode.setCompoundDrawables(img, null, null, null);

        img = getResources().getDrawable(R.drawable.platform_bank_card);
        minimumWidth = img.getMinimumWidth();
        minimumHeight = img.getMinimumHeight();
        img.setBounds(0, 0, minimumWidth, minimumHeight);
        mTvBankCard.setCompoundDrawables(img, null, null, null);
    }

    private void changeHaveDataView(List<PayDetail> list) {
        mAdapter.setData(list);
        if (list != null && list.size() > 0) {
            mListView.setVisibility(View.VISIBLE);
            mNoDataView.setVisibility(View.GONE);
        } else {
            mNoDataView.setVisibility(View.VISIBLE);
            mListView.setVisibility(View.GONE);
        }
    }

    /**
     * 获取列表
     */
    private void initData() {
        showLoadingDialog(R.string.load);
        executeTask(
                () -> {
                    mAllList = getAllSourceData();
                    mQRCodeLists = getQRCodeSourceData();
                    mSwipeList = getSwipeCardSourceData();
                    runOnUI(
                            () -> {
                                dismissLoadingDialog();
                                if (action == TYPE_ALL) {
                                    changeHaveDataView(mAllList);
                                } else if (action == TYPE_CARD) {
                                    changeHaveDataView(mSwipeList);
                                } else if (action == TYPE_CODE) {
                                    changeHaveDataView(mQRCodeLists);
                                }
                            }
                    );
                }
        );
    }


}
